//
//  UIViewController+Tips.m
//  imtMall
//
//  Created by lorin on 15/7/10.
//  Copyright (c) 2015年 IMT. All rights reserved.
//

#import "UIViewController+Tips.h"
#import "MBProgressHUD.h"
#import "MGTipsViewController.h"


static const CGFloat kLabelFontSize = 16.f;

static NSInteger const MB_Progress_Tag       = 100380;
static NSTimeInterval const MB_DEFAULT_DELAY = 2;
static NSString * const MB_DEFAULT_SUCCESS   = @"Completed";

#define RGBAplha(r, g ,b , a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
#define KEY_WINDOW  [UIApplication sharedApplication].keyWindow
/**
 *  提示框容器类别
 */
typedef NS_ENUM(NSUInteger, WF_MBProgress_In_TYPE){
    /**
     *  Controller View
     */
    WF_MBProgress_In_TYPE_Controller,
    /**
     *  Window
     */
    WF_MBProgress_In_TYPE_Window
};



/**
 *  网络请求错误消息类别
 */
typedef NS_ENUM(NSUInteger, WF_NETWORK_REQUEST_ERROR_TYPE){
    WF_NETWORK_REQUEST_ERROR_TYPE_SYSTEM = 1,
    WF_NETWORK_REQUEST_ERROR_TYPE_ALERT,
    WF_NETWORK_REQUEST_ERROR_TYPE_TOAST,
    WF_NETWORK_REQUEST_ERROR_TYPE_PAGE
};

static WF_MBProgress_In_TYPE MBprogressIN = WF_MBProgress_In_TYPE_Controller;


@implementation UIViewController (Tips)

#pragma mark - ShowInViewController
-(void)showWaitingCircleInViewController
{
    [self hiddenToast];
    if (!self.view) {
        return;
    }
    
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    //更改加载图片
//    [[self customViewWithHud:hud] startAnimating];
 
    [self.view addSubview:hud];

    hud.tag = MB_Progress_Tag;
    MBprogressIN = WF_MBProgress_In_TYPE_Controller;
    [hud show:YES];
}

- (UIImageView *)customViewWithHud:(MBProgressHUD *)hud
{
    hud.mode = MBProgressHUDModeCustomView;
    hud.color = RGBAplha(0, 0, 0, 0.6);

    UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    imgV.contentMode = UIViewContentModeScaleAspectFit;
//    imgV.animationImages = [[WFAppConfiguration sharedConfiguration] loadingImages_activityView];
    hud.customView = imgV;
    
    return imgV;
}

-(void)showToastInViewController:(NSString *)message{
    [self showToastInViewController:message withDuration:0];
}

- (void)showToastInViewController:(NSString *)message withDuration:(NSTimeInterval)delay {
    [self showToastInViewController:message withDuration:delay withMask:NO];
}

-(void)showToastInViewController:(NSString *)message withDuration:(NSTimeInterval)delay withMask:(BOOL)mask
{
    [self hiddenToast];
    if (!self.view) {
        return;
    }
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.userInteractionEnabled = mask;//文字信息设置无遮罩
    
    hud.mode = MBProgressHUDModeText;
    hud.detailsLabelText = message;
    hud.detailsLabelFont = [UIFont boldSystemFontOfSize:kLabelFontSize];
    hud.margin = 10.f;
    hud.removeFromSuperViewOnHide = YES;
    
    hud.tag = MB_Progress_Tag;
    delay = delay > 0 ? delay : ([self displayDurationForString:message]);
    MBprogressIN = WF_MBProgress_In_TYPE_Controller;
    
    [hud hide:YES afterDelay:delay];
}


-(void)showSuccessToastInViewController
{
    [self showSuccessToastInViewController:nil withDuration:0];
}

-(void)showSuccessToastInViewController:(NSString *)message
{
    [self showSuccessToastInViewController:message withDuration:0];
}

-(void)showSuccessToastInViewController:(NSString *)message withDuration:(NSTimeInterval)delay {
    [self showSuccessToastInViewController:message withDuration:delay withMask:NO];
}

-(void)showSuccessToastInViewController:(NSString *)message withDuration:(NSTimeInterval)delay withMask:(BOOL)mask
{
    [self hiddenToast];
    if (!self.view) {
        return;
    }
    
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]] ;
    hud.mode = MBProgressHUDModeCustomView;
    hud.detailsLabelText = message?:MB_DEFAULT_SUCCESS;
    hud.detailsLabelFont = [UIFont boldSystemFontOfSize:kLabelFontSize];
    
    
    hud.tag = MB_Progress_Tag;
    delay = delay > 0 ? delay : ([self displayDurationForString:message]);
    MBprogressIN = WF_MBProgress_In_TYPE_Controller;
    
    hud.color = RGBAplha(0, 0, 0, 0.6);
    
    hud.userInteractionEnabled = mask;//文字信息设置无遮罩
    
    [hud show:YES];
    [hud hide:YES afterDelay:delay];
}


#pragma mark - ShowInWindow
-(void)showWaitingCircleInWindow
{
    [self hiddenToast];
    if (!KEY_WINDOW) {
        return;
    }
    
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:KEY_WINDOW];
    //更改加载图片
//    [[self customViewWithHud:hud] startAnimating];
    
    [KEY_WINDOW addSubview:hud];
    
    hud.tag = MB_Progress_Tag;
    MBprogressIN = WF_MBProgress_In_TYPE_Window;
    [hud show:YES];
}

-(void)showToastInWindow:(NSString *)message
{
    [self showToastInWindow:message withDuration:0];
}

-(void)showToastInWindow:(NSString *)message withDuration:(NSTimeInterval)delay {
    [self showToastInWindow:message withDuration:delay withMask:NO];
}

-(void)showToastInWindow:(NSString *)message withDuration:(NSTimeInterval)delay withMask:(BOOL)mask
{
    [self hiddenToast];
    if (!KEY_WINDOW) {
        return;
    }
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:KEY_WINDOW animated:YES];
    hud.userInteractionEnabled = mask;//文字信息设置无遮罩
    hud.mode = MBProgressHUDModeText;
    hud.detailsLabelText = message;
    hud.detailsLabelFont = [UIFont boldSystemFontOfSize:kLabelFontSize];
    hud.margin = 10.f;
    hud.removeFromSuperViewOnHide = YES;
    
    hud.tag = MB_Progress_Tag;
    delay = delay > 0 ? delay : ([self displayDurationForString:message]);
    MBprogressIN = WF_MBProgress_In_TYPE_Window;
    
    [hud hide:YES afterDelay:delay];
}

-(void)showSuccessToastInWindow
{
    [self showSuccessToastInViewController:nil withDuration:0];
}

-(void)showSuccessToastInWindow:(NSString *)message
{
    [self showSuccessToastInViewController:message withDuration:0];
}

-(void)showSuccessToastInWindow:(NSString *)message withDuration:(NSTimeInterval)delay {
    [self showSuccessToastInWindow:message withDuration:delay withMask:NO];
}

-(void)showSuccessToastInWindow:(NSString *)message withDuration:(NSTimeInterval)delay withMask:(BOOL)mask
{
    [self hiddenToast];
    if (!KEY_WINDOW) {
        return;
    }

    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:KEY_WINDOW];
    [KEY_WINDOW addSubview:hud];
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]] ;
    hud.mode = MBProgressHUDModeCustomView;
    hud.detailsLabelText = message?:MB_DEFAULT_SUCCESS;
    hud.detailsLabelFont = [UIFont boldSystemFontOfSize:kLabelFontSize];
    
    
    hud.tag = MB_Progress_Tag;
    delay = delay > 0 ? delay : ([self displayDurationForString:message]);
    MBprogressIN = WF_MBProgress_In_TYPE_Window;
    
    hud.color = RGBAplha(0, 0, 0, 0.6);
    hud.userInteractionEnabled = mask;//文字信息设置无遮罩
    [hud show:YES];
    [hud hide:YES afterDelay:delay];
}

#pragma mark - hiddenView
-(void)hiddenToast
{
    if (MBprogressIN == WF_MBProgress_In_TYPE_Controller)
    {
        [self hiddenToastInViewController];
    }
    else
    {
        [self hiddenToastInWindow];
    }
}

-(void)hiddenAllToast
{
    [self hiddenToastInViewController];
    [self hiddenToastInWindow];
}

-(void)hiddenToastInViewController
{
    for (UIView *view in self.view.subviews) {
        if (view.tag == MB_Progress_Tag) {
            [view removeFromSuperview];
            break;
        }
    }
}

-(void)hiddenToastInWindow
{
    for (UIView *view in KEY_WINDOW.subviews) {
        if (view.tag == MB_Progress_Tag) {
            [view removeFromSuperview];
            break;
        }
    }
}

#pragma mark - Hanlder network request error
-(void)showNetworkRequestMessage:(NSString *)message errorCode:(NSInteger)errorCode
{
    
    [self hiddenAllToast];
    
//    if (<#condition#>) {
//        <#statements#>
//    }
//    MGTipsViewController * Vc=[MGTipsViewController viewWithSelfNib];
//    Vc.tipsViewType=type;
//    [Vc customerUI];
//    [self.view addSubview:Vc];
}

-(void)showNetworkOfflineView{
    
   
}

-(void)hiddenNetworkStatusView{

}

-(void)showTipsView:(MGTipsViewType )type content:(NSString *)string{
    
    MGTipsViewController * Vc=[MGTipsViewController viewWithSelfNib];
    Vc.tipsViewType=type;
    Vc.noResult_Lab.text=string;
    
    [Vc customerUI];
    
    [KEY_WINDOW addSubview:Vc];
    [Vc mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(KEY_WINDOW).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}
-(void)showTips:(MGTipsViewType )type content:(NSString *)string message:(NSString *)message andCtr:(UIViewController *)ctr andType:(NSInteger)types
{
    MGTipsViewController * Vc=[MGTipsViewController viewWithSelfNib];
    Vc.tipsViewType=type;
    Vc.noResult_Lab.text=string;
    Vc.message1 = message;
    Vc.types = types;
    

    [Vc customerUI];
    
    [ctr?ctr.view:KEY_WINDOW addSubview:Vc];
    [Vc mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(ctr?ctr.view:KEY_WINDOW).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];

}
#pragma mark -- 文字显示时间 --
- (NSTimeInterval)displayDurationForString:(NSString*)string {
    
    CGFloat missTime = MIN((CGFloat)string.length*0.06 + 0.3, 3.5);
    return MAX(missTime, MB_DEFAULT_DELAY);
}



@end
